package com.byxy.dao;

import com.byxy.model.Administrators;
import com.byxy.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdministratorsDaoImpl implements BaseDao<Administrators> {
	private PreparedStatement psta = null;
	private Connection conn = null;
	private ResultSet rs = null;

	@Override
	public int add(Administrators admin) {
		int i = -1;
		int id = admin.getId();
		String loginName = admin.getLoginName();
		String loginPassword = admin.getLoginPassword();
		int departmentId = admin.getDepartmentId();
		String sql = "INSERT INTO Administrators VALUES(?, ?, ?, ?)";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			psta.setString(2, loginName);
			psta.setString(3, loginPassword);
			psta.setInt(4, departmentId);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public int deleteById(int id) {
		int i = -1;
		String sql = "DELETE FROM Administrators WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public int update(Administrators admin) {
		int i = -1;
		int id = admin.getId();
		String loginName = admin.getLoginName();
		String loginPassword = admin.getLoginPassword();
		int departmentId = admin.getDepartmentId();
		String sql = "UPDATE Administrators SET loginName = ?, loginPassword = ?, departmentId = ? WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setString(1, loginName);
			psta.setString(2, loginPassword);
			psta.setInt(3, departmentId);
			psta.setInt(4, id);
			i = psta.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	@Override
	public List<Administrators> findAll() {
		List<Administrators> list = new ArrayList<Administrators>();
		String sql = "SELECT id,loginName,departmentId FROM Administrators";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				String loginName = rs.getString("loginName");
				int departmentId = rs.getInt("departmentId");
				list.add(new Administrators(id, loginName, departmentId));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public Administrators findById(int id) {
		Administrators admin = null;
		String sql = "SELECT id,loginName,departmentId FROM Administrators WHERE id = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, id);
			rs = psta.executeQuery();
			while (rs.next()) {
				int uid = rs.getInt("id");
				String loginName = rs.getString("loginName");
				int departmentId = rs.getInt("departmentId");
				admin = new Administrators(uid, loginName, departmentId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return admin;
	}

	/**
	 * 登录
	 * @param admin
	 * @return
	 */
	public boolean login(Administrators admin) {
		boolean result = false;
		String loginName = admin.getLoginName();
		String loginPassword = admin.getLoginPassword();
		String sql = "SELECT loginName,loginPassword FROM Administrators WHERE loginName = ? AND loginPassword = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setString(1, loginName);
			psta.setString(2, loginPassword);
			rs = psta.executeQuery();
			String correctLoginName = null;
			String correctLoginPassword = null;
			while (rs.next()) {
				correctLoginName = rs.getString("loginName");
				correctLoginPassword = rs.getString("loginPassword");
			}
			if (!(correctLoginName == null || correctLoginPassword == null)) {
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 修改密码
	 * @param username
	 * @param oldPassword
	 * @param newPassword
	 * @return
	 */
	public boolean alterPassword(String username, String oldPassword, String newPassword) {
		boolean result = false;
		String sql = "SELECT loginName,loginPassword FROM Administrators WHERE loginName = ?";
		try {
			conn = DBUtil.getConnection();
			psta = conn.prepareStatement(sql);
			psta.setString(1, username);
			rs = psta.executeQuery();
			String correctLoginPassword = null;
			while (rs.next()) {
				correctLoginPassword = rs.getString("loginPassword");
			}
			if (oldPassword.equals(correctLoginPassword)) {
				String sql2 = "UPDATE Administrators SET loginPassword = ? WHERE loginName = ?";
				psta = conn.prepareStatement(sql2);
				psta.setString(1, newPassword);
				psta.setString(2, username);
				psta.executeUpdate();
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.close(rs, psta, conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
}
